package com.reny.ll.git.base_logic

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.reny.git.imgload.glide.ImgUtils
import com.reny.ll.git.mvvm.MvvmDelegateImp
import org.greenrobot.eventbus.EventBus

class MDelegateImp(private val master: Any): MvvmDelegateImp(master) {

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    override fun onCreate(owner: LifecycleOwner?) {
        if(master is MActivity<*>){
            master.injectARouter()
            //AppManager.instance.addActivity(owner)
            master.setStatusBar(master.isTransStatusBar())

            //该方法是【友盟+】Push后台进行日活统计及多维度推送的必调用方法，请务必调用！
            //PushAgent.getInstance(owner).onAppStart()
        }

        if(master is IUseEventBus){
            if(master.useEventBus()) {
                EventBus.getDefault().register(master)
            }
        }
        super.onCreate(owner)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    override fun onStart(owner: LifecycleOwner?) {
        super.onStart(owner)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    override fun onResume(owner: LifecycleOwner?) {
        super.onResume(owner)
        if(master is MActivity<*>) {
            ImgUtils.instance.resumeRequests(master)
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    override fun onStop(owner: LifecycleOwner?) {
        super.onStop(owner)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    override fun onPause(owner: LifecycleOwner?) {
        super.onPause(owner)
        if(master is MActivity<*>) {
            ImgUtils.instance.pauseRequests(master)
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    override fun onDestroy(owner: LifecycleOwner?) {
        super.onDestroy(owner)
        if(master is IUseEventBus){
            if(master.useEventBus() && EventBus.getDefault().isRegistered(master)){
                EventBus.getDefault().unregister(master)
            }
        }

        if(master is MActivity<*>){
            master.currentFocus?.isFocusable = false
            master.currentFocus?.isFocusableInTouchMode = false
            master.hideSoftKeyboard()

            master.cancelProgressDialog()
        }

        /*if(null != mCompositeDisposable) {
            mCompositeDisposable?.clear()
            mCompositeDisposable = null
        }*/
    }

    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        if(master is MActivity<*>){
            ImgUtils.instance.onTrimMemory(master, level)
        }
    }

    override fun onLowMemory() {
        super.onLowMemory()
        if(master is MActivity<*>){
            ImgUtils.instance.onLowMemory(master)
        }
    }

    /*var mCompositeDisposable: CompositeDisposable? = null

    private fun addDisposable(disposable: Disposable?) {
        if (null == mCompositeDisposable) mCompositeDisposable = CompositeDisposable()
        mCompositeDisposable?.add(disposable)
    }

    fun delay(time: Long, callback:()->Unit): Disposable {
        val disposable = Observable.timer(time, TimeUnit.MILLISECONDS)
            .subscribe {
                callback()
            }
        addDisposable(disposable)
        return disposable
    }

    //计时器
    fun interval(maxSeconds:Long = 60L, callback:(Long)->Unit){
        val timer = Observable.interval(0,1, TimeUnit.SECONDS).take(maxSeconds)
            .subscribe {
                callback(it)
            }
        addDisposable(timer)
    }*/

}